﻿using System;
using System.Xml;
using InfoJetSoft.Service.Entity;

namespace InfoJetSoft.Service.Util
{
	
	class TimeFormater
	{
		
		public static string format(Form form, XmlNode node, string value_Renamed, TypeFormat format, bool isDeformat)
		{
			if (value_Renamed.Trim().Length <= 0)
			{
				return "";
			}
			string timeFormat = format.TimeFormat;
			if (timeFormat == null)
			{
				timeFormat = "H:mm:ss";
			}
			if (format.NoSeconds != null && format.NoSeconds.Equals("1"))
			{
				timeFormat = timeFormat.Replace(":ss", "");
				timeFormat = timeFormat.Replace(":s", "");
			}
			SimpleDateFormat sdf = new SimpleDateFormat(timeFormat, LocaleUtil.getLocale(format));
			System.DateTime date;
			try
			{
				date = sdf.parse( value_Renamed );
			}
			catch (System.FormatException e1)
			{
				try
				{
					string trySSFormat = "H:mm:ss";
					if (format.NoSeconds != null && format.NoSeconds.Equals("1")) {
						trySSFormat = trySSFormat.Replace(":ss", "");
					}
					SimpleDateFormat trySSSDF = new SimpleDateFormat(trySSFormat);
					date = trySSSDF.parse( value_Renamed );
				}
				catch (System.FormatException e2)
				{
					try
					{
						string tryFormat = "H:mm";
						SimpleDateFormat trySDF = new SimpleDateFormat(tryFormat);
						date = trySDF.parse( value_Renamed );
					}
					catch (System.FormatException e3)
					{
						return value_Renamed;
					}
				}
			}
			SimpleDateFormat storeFormat = new SimpleDateFormat("HH:mm:ss");
			string nodeValue = storeFormat.format(date);

			if( isDeformat )
			{
				FormUtil.updateFormattingNode(form, node, nodeValue);
			}
			return sdf.format(date);
		}
	}
}